package com.hxd.otc.module.order.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hxd.otc.R;
import com.hxd.otc.module.order.bean.CheckImageBean;
import com.hxd.otc.utils.GlideUtils;

import java.util.List;

/**
 * 包  名 :  com.hxd.otc.module.deal.adapter
 * 类  名 :  ImageChooseAdapter
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/8 16:30
 * 描  述 :  ${TODO} 图片选择Adapter
 */
public class ImageChooseAdapter extends RecyclerView.Adapter<ImageChooseAdapter.ViewHolder> {
    public List<CheckImageBean> mData;
    public Context mContext;
    private static final int CHOOSE_MAX_IMAGE = 3;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        this.onImageClickListener = onImageClickListener;
    }

    private OnImageClickListener onImageClickListener;

    public ImageChooseAdapter(@NonNull Context context, @NonNull List<CheckImageBean> data) {
        this.mContext = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_complain_proof_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        if (mData.get(position).isImage) {
            GlideUtils.setImage(mData.get(position).imagePath, holder.mCameraImage);
            holder.mCameraImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.mDeleteImage.setVisibility(View.VISIBLE);
        } else {
            holder.mCameraImage.setImageDrawable(ContextCompat.getDrawable(mContext, mData.get(position).resImage));
            holder.mCameraImage.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.mDeleteImage.setVisibility(View.GONE);
        }
        holder.mDeleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.remove(position);
                if (onImageClickListener != null) {
                    onImageClickListener.onDelete(holder.mDeleteImage);
                }
                notifyDataSetChanged();
            }
        });
        holder.mCameraImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData.get(position).isImage) {
                    if (onImageClickListener != null) {
                        onImageClickListener.onPreview(holder.mCameraImage, position);
                    }
                } else {
                    onImageClickListener.onAdd(holder.mCameraImage);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size() > CHOOSE_MAX_IMAGE ? CHOOSE_MAX_IMAGE : mData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mCameraImage;
        private ImageView mDeleteImage;

        public ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            mCameraImage = itemView.findViewById(R.id.camera_iv);
            mDeleteImage = itemView.findViewById(R.id.delete_iv);
        }
    }

    public interface OnImageClickListener {
        void onPreview(View view, int position);

        void onAdd(View view);

        void onDelete(View view);
    }
}
